package gateway;

import java.io.IOException;
import java.util.Date;

import org.jsmpp.InvalidResponseException;
import org.jsmpp.PDUException;
import org.jsmpp.bean.BindType;
import org.jsmpp.bean.ESMClass;
import org.jsmpp.bean.GeneralDataCoding;
import org.jsmpp.bean.NumberingPlanIndicator;
import org.jsmpp.bean.RegisteredDelivery;
import org.jsmpp.bean.SMSCDeliveryReceipt;
import org.jsmpp.bean.TypeOfNumber;
import org.jsmpp.extra.NegativeResponseException;
import org.jsmpp.extra.ResponseTimeoutException;
import org.jsmpp.session.BindParameter;
import org.jsmpp.session.SMPPSession;
import org.jsmpp.util.AbsoluteTimeFormatter;
import org.jsmpp.util.TimeFormatter;

public class MessageGateway implements IMessageGateway {

	
	private static TimeFormatter timeFormatter = new AbsoluteTimeFormatter();;
	
	SMPPSession smppSession;
	BindParameter bp;	
	
	public MessageGateway()
	{
	     bp =
	    		new BindParameter( 
		        					BindType.BIND_TX,
		        					SMPPClientConfig.LOGIN , SMPPClientConfig.PASSWORD , "cp",
		        					TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN,
		        					SMPPClientConfig.ADDRESS_RANGE 
	        					 );
	    
	    smppSession = new SMPPSession();		
	}
	
	
	public String sendMessage( String fromPhoneNumber , String toPhoneNumber, String messageBody ) throws Exception
	{
        try 
        {
            String messageId = smppSession.submitShortMessage(
                    "CMT", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, fromPhoneNumber , TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, toPhoneNumber , new ESMClass(), (byte)0, (byte)1,
                                                timeFormatter.format(new Date()), null, new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS_FAILURE), (byte)0,
                                                new GeneralDataCoding()
                                                , (byte)0, messageBody.getBytes()
                                            );

            /*
             * you can save the submitted message to database.
             */

            System.out.println("Message submitted, message_id is " + messageId);


            return messageId;
            
        } catch (PDUException ex) {
            // Invalid PDU parameter
            System.err.println("Invalid PDU parameter");
            ex.printStackTrace();
            throw new Exception(ex);
        } catch (ResponseTimeoutException eto) {
            // Response timeout
            System.err.println("Response timeout");
            eto.printStackTrace();
            throw new Exception(eto);
        } catch (InvalidResponseException eir) {
            // Invalid response
            System.err.println("Receive invalid respose");
            eir.printStackTrace();
            throw new Exception(eir);
        } catch (NegativeResponseException ene) {
            // Receiving negative response (non-zero command_status)
            System.err.println("Receive negative response");
            ene.printStackTrace();
            throw new Exception(ene);
        } catch (IOException eio) {
            System.err.println("IO error occur");
            eio.printStackTrace();
            throw new Exception(eio);
        }
	}

	public void bind() throws Exception 
	{
		smppSession.connectAndBind( SMPPClientConfig.HOST , SMPPClientConfig.PORT , bp );
	}

	public void unbind() {
		smppSession.unbindAndClose();
		
	}
}
